package com.typingtutor.ui.compose;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonTabComponent extends JPanel {
    private final JTabbedPane pane;

    public ButtonTabComponent(final JTabbedPane pane) {
        // 设置布局为 FlowLayout
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (pane == null) {
            throw new NullPointerException("TabbedPane is null");
        }
        this.pane = pane;
        // 设置不透明
        setOpaque(false);

        // 创建标签文本标签
        JLabel label = new JLabel() {
            @Override
            public String getText() {
                int i = pane.indexOfTabComponent(ButtonTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                }
                return null;
            }
        };

        // 添加标签文本标签
        add(label);
        // 添加间距
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        // 创建关闭按钮
        JButton button = new TabButton();
        // 添加关闭按钮
        add(button);
    }

    // 关闭按钮类
    private class TabButton extends JButton implements ActionListener {
        public TabButton() {
            // 获取当前标签页的索引
            int i = pane.indexOfTabComponent(ButtonTabComponent.this);
            if (i != 0) {
                int size = 17;
                // 设置按钮大小
                setPreferredSize(new Dimension(size, size));
                // 设置按钮边界
                setToolTipText("关闭此标签页");
                // 设置按钮不绘制边框
                setContentAreaFilled(false);
                // 设置按钮不聚焦
                setFocusable(false);
                // 设置按钮边框为空
                setBorder(BorderFactory.createEtchedBorder());
                // 设置按钮边框为圆形
                setBorderPainted(false);
                // 添加按钮事件监听器
                addActionListener(this);
            }
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // 获取当前标签页的索引
            int i = pane.indexOfTabComponent(ButtonTabComponent.this);
            if (i != 0) {
                // 删除对应的标签页
                pane.remove(i);
            }
        }

        // 绘制关闭按钮的图标
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // 获取当前标签页的索引
            int i = pane.indexOfTabComponent(ButtonTabComponent.this);
            if (i != 0) {
                Graphics2D g2 = (Graphics2D) g.create();
                // 设置抗锯齿
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                // 获取按钮的宽度和高度
                int width = getWidth();
                int height = getHeight();
                // 绘制关闭图标
                g2.setStroke(new BasicStroke(2));
                g2.setColor(Color.GRAY);
                if (getModel().isRollover()) {
                    g2.setColor(Color.RED);
                }
                int delta = 6;
                g2.drawLine(delta, delta, width - delta - 1, height - delta - 1);
                g2.drawLine(width - delta - 1, delta, delta, height - delta - 1);
                g2.dispose();
            }
        }
    }
}